package com.wdh.testq.tools;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/**
 * Created by Gss on 2018/3/30 0030.
 */

public class SettingTools {
    private static SettingTools INSTANCE;

    public static SettingTools getInstance() {
        if (INSTANCE == null)
            INSTANCE = new SettingTools();
        return INSTANCE;
    }

    public void goSetting(Context context) {
        if (RomUtils.isXiaomi()) {
            jumpMiuiSetting(context);
        } else if (RomUtils.isHuawei()) {
            jumpHuaweiSetting(context);
        } else if (RomUtils.isMeizu()) {
            jumpFlymeSetting(context);
        } else
            jumpDefaultSetting(context);
    }

    private void jumpDefaultSetting(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        context.startActivity(localIntent);
    }


    private void jumpFlymeSetting(Context context) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("packageName", context.getPackageName());
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            jumpDefaultSetting(context);
        }
    }

    private void jumpHuaweiSetting(Context context) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            jumpDefaultSetting(context);
        }
    }

    private void jumpMiuiSetting(Context context) {
        Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
        ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
        i.setComponent(componentName);
        i.putExtra("extra_pkgname", context.getPackageName());
        try {
            context.startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
            jumpDefaultSetting(context);
        }
    }
}
